<?php
declare(strict_types=1);
/**
 * MineAdmin is committed to providing solutions for quickly building web applications
 * Please view the LICENSE file that was distributed with this source code,
 * For the full copyright and license information.
 * Thank you very much for using MineAdmin.
 *
 * @Author X.Mo<root@imoi.cn>
 * @Link   https://gitee.com/xmo/MineAdmin
 */

namespace Plugin\Device\Service;

use Common\Common;
use Plugin\Device\Repository\RdDeviceBroadcastServerRepository;
use App\Service\IService;

/**
 * 广播服务器服务类
 */
final class RdDeviceBroadcastServerService extends IService
{
    public function __construct(
        protected readonly RdDeviceBroadcastServerRepository $repository
    ) {}

    public function create(array $data): mixed
    {
        // 加密密码和认证字段
        $aesKey = env('AES_KEY', '1234567890123456');
        $data['password'] = Common::aesEncrypt($data['password'], $aesKey);
        $data['auth'] = Common::aesEncrypt($data['auth'], $aesKey);
        return parent::create($data);
    }
    
    public function updateById(mixed $id, array $data): mixed
    {
        // 更新时加密密码和认证字段
        $aesKey = env('AES_KEY', '1234567890123456');
        if (isset($data['password'])) {
            $data['password'] = Common::aesEncrypt($data['password'], $aesKey);
        }
        if (isset($data['auth'])) {
            $data['auth'] = Common::aesEncrypt($data['auth'], $aesKey);
        }
        return parent::updateById($id, $data);
    }
}